package com.tiantian.web.controller.audit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiantian.bean.PageBean;
import com.tiantian.cache.RegionalInformationCache;
import com.tiantian.dao.PlatformCreditRatingRepository;
import com.tiantian.domain.Auditprocessinfo;
import com.tiantian.domain.Bindingplatformaccounts;
import com.tiantian.domain.Businessstore;
import com.tiantian.domain.Buyersusers;
import com.tiantian.domain.Platformcreditrating;
import com.tiantian.ret.Result;
import com.tiantian.service.AuditService;
import com.tiantian.service.BindingPlatformAccountsService;
import com.tiantian.service.BusinessStoreService;
import com.tiantian.service.BuyersUsersService;
import com.tiantian.service.TaskinformationService;
import com.tiantian.util.PageUtil;
import com.tiantian.web.datatables.DataTableReturn;

/**
 * 审核管理
 * 
 */
@Controller
@RequestMapping("/audit")
public class AuditController {
	public static final Logger LOG = LoggerFactory.getLogger(AuditController.class);
	@Resource
	private TaskinformationService taskInfoService;
	@Resource
	private AuditService auditService;
	@Resource
	private BuyersUsersService buyersUsersService;
	@Resource
	private BindingPlatformAccountsService bindingPlatformAccountsService;
	@Autowired
	private BusinessStoreService businessStoreService;
	@Autowired
	private RegionalInformationCache regionalInformationCache;
	@Autowired
	private PlatformCreditRatingRepository platformCreditRatingRepository;

	/**
	 * 审核管理
	 * 
	 * @param tipBuyersNotes
	 * @return
	 */
	@RequestMapping(value = { "/index", "/manager", "/list", "/", "" })
	public void index(Model model) {

	}

	@ResponseBody
	@RequestMapping(value = { "auditList" })
	public Object auditList(AuditVo auditVo, HttpServletRequest request) {
		PageBean page = PageUtil.parseRequest2PageBean(request);
		Map<String, String> params = null;
		try {
			params = BeanUtils.describe(auditVo);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			params = new HashMap<String, String>();
		}
		List<Auditprocessinfo> data = auditService.list(params, page);
		return new DataTableReturn(page.getDraw(), page.getTotal(), page.getTotal(), data);
	}

	@RequestMapping(value = { "/detail" })
	public String detail(Model model, @RequestParam String id, @RequestParam String type, @RequestParam String auditAssociationId) {
		model.addAttribute("audit", auditService.get(id));
		switch (type) {
		case "1":// 用户账号绑定
			Buyersusers buyersusers = buyersUsersService.get(auditAssociationId);
			model.addAttribute("data", buyersusers);
			return "audit/detail1";
		case "2":// 买手绑定平台账号审核
			Bindingplatformaccounts bindingplatformaccounts = bindingPlatformAccountsService.get(auditAssociationId);
			Buyersusers user = buyersUsersService.get(bindingplatformaccounts.getBuyerUserId());
			model.addAttribute("accounts", bindingplatformaccounts);
			model.addAttribute("user", user);
			model.addAttribute(
					"address",
					regionalInformationCache.getAreaName(bindingplatformaccounts.getProvince())
							+ regionalInformationCache.getAreaName(bindingplatformaccounts.getCity())
							+ regionalInformationCache.getAreaName(bindingplatformaccounts.getArea()) + bindingplatformaccounts.getShippingAddress());
			List<Platformcreditrating> platformcreditratings = platformCreditRatingRepository.findByCreditRatingNo(bindingplatformaccounts
					.getCreditRatingNo());
			if (platformcreditratings.size() > 0) {
				model.addAttribute("platformCreditRating", platformcreditratings.get(0));
			}
			return "audit/detail2";
		case "3":// 商家绑定商铺审核
			Businessstore businessstore = businessStoreService.get(auditAssociationId);
			model.addAttribute("data", businessstore);
			return "audit/detail3";
		case "4":// 买号审核
			Bindingplatformaccounts bindingplatformaccounts1 = bindingPlatformAccountsService.get(auditAssociationId);
			Buyersusers user1 = buyersUsersService.get(bindingplatformaccounts1.getBuyerUserId());
			model.addAttribute("accounts", bindingplatformaccounts1);
			model.addAttribute("user", user1);
			return "audit/detail2";
		default:
			break;
		}
		return null;

	}

	@RequestMapping(value = { "/edit" })
	@ResponseBody
	public Result<?> edit(@RequestParam String id, @RequestParam Integer auditStatus, String auditOpinion) {

		try {
			return auditService.edit(id, auditStatus, auditOpinion);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result<Object>(false, "失败成功");
		}
	}
}
